package com.sahadev.mvp.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sahadev.mvp.MVPActivity;
import com.sahadev.mvp.model.AbstIData;
import com.sahadev.mvp.other.BaseRecyclerAdapter;
import com.sahadev.mvp.view.IView;

import org.json.JSONObject;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Sahadev on 2015/11/12.
 *
 * @deprecated 这个Presenter不应该实现IRecyclerView, 应当由子类去实现,如果要用它实现更大的功能的话
 */
public abstract class Presenter implements Handler.Callback, MVPActivity.IRecyclerView {
    protected AbstIData abstIData;
    protected IView iView;

    /**
     * @param iView 为展示的Activity,其中abstIData请手动实例化
     */
    public Presenter(IView iView) {
        this.iView = iView;
        //请在子类中实例化IData接口
    }

    public final void requestData(int index) {
        abstIData.requestData(index);
    }

    /**
     * 用来识别是否成功被调用
     *
     * @return
     */
    public abstract String getTag();

    /**
     * 获取将要被解析的类，就是要求在数据解析的时候，被解析成什么类型
     *
     * @return
     */
    public abstract Type getInstanceType();

    @Override
    public boolean handleMessage(Message msg) {
        Object obj = msg.obj;
        switch (msg.what) {
            case 0:
                onSuccess(obj);
                return true;
            case 1:
                onFailed(obj);
                return false;
        }
        return false;
    }

    protected void onSuccess(Object obj) {
        //可以在这里执行数据解析,再将解析好的数据给iView
        iView.onSuccess(obj);
    }

    protected void onFailed(Object obj) {
        iView.onFailed(obj);
    }
}
